var deleteUrl = "";
let startTime = "";
let endTime = "",
    type = 38,
    maximgsize = 100;
let types = [{ "id": 39, "text": "茶言说：页面以方形排版的形式，开放客户上传视频，图片，茶评，相互评论自由度高，管理负责审核通过任何评论;" },
    { "id": 40, "text": "茶精选：推文形式，介绍宜关品牌的产品，产品上加链接专去购买页面，同时开放评论以及点赞;" },
    { "id": 38, "text": "茶文化：长条形排版，精选选公众号阅读量高的文章投放茶叶相关知识，只开设评论和点赞;" },
];
layui.config({
    version: '1510164124181'
}).use(['laypage', 'laydate', 'layer', 'table', 'form', 'element', 'upload'], function() {
    table = layui.table, laypage = layui.laypage, layer = layui.layer, laydate = layui.laydate, upload = layui.upload, element = layui.element, form = layui.form;
    //监听折叠
    element.on('collapse(test)', function(data) {
        layer.msg('展开状态：' + data.show);
    });
    let active = {
        getAddData: function() { //添加
            AddInfo("");
        },
        getCheckData: function() { //批量删除
            let checkStatus = table.checkStatus('tableInfoId'),
                data = checkStatus.data;
            let dataId = "";
            for (var i = 0; i < data.length; i++) {
                dataId += data[i].id + ",";
            }
            if (dataId.length > 0) {
                dataId = dataId.substring(0, dataId.length - 1);
                if (type == 38) {
                    deleteUrl = "delFindArticleById";
                } else if (type == 40) {
                    deleteUrl = "delFindTeatal";
                }
                DeleteAjaxHttp(dataId, deleteUrl);
            } else {
                top.layer.msg("请选择你要删除的数据!!");
            }
        }
    };

    form.on('select(selectState)', function(obj) {
        var id = $(obj.elem).attr("data-id");
        var val = obj.value;
        updateFindStateInfoById(id, val);
    });

    $('.demoTable .layui-btn').on('click', function() {
        let type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
    //监听操作
    form.on('switch(stateSelectDemo)', function(obj) {
        pageNo = 1;
        inituserdata();
    });

    //监听操作
    form.on('switch(typeSelectDemo)', function(obj) {
        pageNo = 1;
        inituserdata();
    });
    // updStateFindTeatal
    //监听搜索操作
    form.on('submit(formSubSearch)', function(obj) {
        pageNo = 1;
        inituserdata();
    });
    //监听工具条
    table.on('tool(tableInfo)', function(obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            if (type == 38) {
                deleteUrl = "delFindArticleById";
            } else if (type == 40) {
                deleteUrl = "delFindTeatal";
            }
            DeleteAjaxHttp(data.id, deleteUrl);
        } else if (obj.event === 'edit') {
            AddInfo(data.id);
        } else if (obj.event === 'detail') {
            detailInfo(data.id);
        }
    });

    //监听单元格编辑
    table.on('edit(tableInfo)', function(obj) {
        let value = obj.value //得到修改后的值
            ,
            data = obj.data //得到所在行所有键值
            ,
            field = obj.field; //得到字段
        layer.confirm("您确定将此条信息修改为:" + value + "?", { icon: 3, title: '提示信息' }, function(index) {
            index = top.layer.msg('提交中，请稍候', { icon: 16, time: false, shade: 0.8 });
            editInfo(data.id, field, value);
        });
    });
    inituserdata();
});

function inituserdata() {
    $.ajax({
        url: httpurl + "/pagePayVoucherCall?pageSize=" + pageSize + "&page=" + pageNo + "&openid=&name=&phone=",
        cache: false,
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "Authorization",
            "Authorization": GetTokenInfo()
        },
        contentType: 'application/json',
        success: function(data) {
            let dataInfo = [];
            totalRecords = 0;
            if (data.rtnCode == 200) {
                dataInfo = data.data.list;
                totalRecords = data.data.total;
            }
            table.render({
                elem: '#tableInfo',
                data: dataInfo,
                id: "tableInfoId",
                cols: [
                    [
                        { field: 'id', align: "center", title: 'ID', sort: true },
                        { field: 'orderid', align: "center", title: '订单号', sort: true },
                        { field: 'phone', align: "center", title: '电话号码', sort: true },
                        { field: 'username', align: "center", title: '名称', sort: true },
                        { field: 'nickname', align: "center", title: '微信昵称', sort: true }, {
                            field: 'headimg',
                            align: "center",
                            title: '头像',
                            templet: function(res) {
                                return '<div onclick="show_img(this)"><img style="width:100px; height:100px;" src="' + res.headimg + '"/></div>';
                            }
                        }, {
                            field: 'image',
                            align: "center",
                            title: '支付凭证1',
                            templet: function(res) {
                                return '<div onclick="show_img(this)"><img class="width40" src="' + res.image + '"/></div>';
                            }
                        }, {
                            field: 'payImage',
                            align: "center",
                            title: '支付凭证2',
                            templet: function(res) {
                                return '<div onclick="show_img(this)"><img class="width100" src="' + res.payImage + '"/></div>';
                            }
                        }

                        // , {field: 'del', align: "center", width: 200, title: '操作', templet: '#barDemo', unresize: true}
                    ]
                ]
            });
            top.layer.closeAll();
            pageLayPage("inituserdata");
        }
    });
}

function getlistdata(openid, name, phone) {
    $.ajax({
        url: httpurl + "/pagePayVoucherCall",
        cache: false,
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "Authorization",
            "Authorization": GetTokenInfo()
        },
        data: {
            "pageSize": pageSize,
            "page": pageNo,
            "openid": openid ? openid : '',
            "name": name ? name : '',
            "phone": phone ? phone : ''
        },
        contentType: 'application/json',
        success: function(data) {
            let dataInfo = [];
            totalRecords = 0;
            if (data.rtnCode == 200) {
                dataInfo = data.data.list;
                totalRecords = data.data.total;
            }
            table.render({
                elem: '#tableInfo',
                data: dataInfo,
                id: "tableInfoId",
                cols: [
                    [
                        // { field: 'openid', align: "center", title: 'OPENID', sort: true },
                        { field: 'orderid', align: "center", title: '订单号', sort: true },
                        { field: 'phone', align: "center", title: '电话号码', sort: true },
                        { field: 'username', align: "center", title: '名称', sort: true },
                        { field: 'nickname', align: "center", title: '微信昵称', sort: true }, {
                            field: 'headimg',
                            align: "center",
                            title: '头像',
                            templet: function(res) {
                                return '<div onclick="show_img(this)"><img style="width:100px; height:100px;" src="' + res.headimg + '"/></div>';
                            }
                        }, {
                            field: 'image',
                            align: "center",
                            title: '支付凭证1',
                            templet: function(res) {
                                return '<div onclick="show_img(this)"><img class="width40" src="' + res.image + '"/></div>';
                            }
                        }, {
                            field: 'payImage',
                            align: "center",
                            title: '支付凭证2',
                            templet: function(res) {
                                return '<div onclick="show_img(this)"><img class="width100" src="' + res.payImage + '"/></div>';
                            }
                        }
                    ]
                ]
            });
            top.layer.closeAll();
            pageLayPage("inituserdata");
        }
    });
}

function exportdata() {
    var name = $("#username").val() || '';
    var phone = $("#phoneNum").val() || '';
    console.log(name, phone);
    window.location.href = httpurl + '/ExportPayVoucherExcel?openid=' + '&name=' + name + '&phone=' + phone + '&_=1602068497808'
}

function show_img(e) {
    var e = $(e).find("img");
    //页面层
    // layer.open({
    //     type: 1,
    //     skin: 'layui-layer-rim', //加上边框
    //     area: ['80%', '80%'], //宽高
    //     shadeClose: true, //开启遮罩关闭
    //     end: function(index, layero) {
    //         return false;
    //     },
    //     content: '<div style="text-align:center"><img src="' + $(e).attr('src') + '" /></div>'
    // });
    layer.open({
        type: 1,
        title: false,
        closeBtn: 0,
        shadeClose: true, //点击阴影关闭
        //area: [$(e).width + 'px', $(e).height + 'px'], //宽高
        area: ['80%', '80%'], //宽高
        content: '<div style="text-align:center"><img style="max-width:100%;max-height:100%;" src="' + $(e).attr('src') + '" /></div>'
    });
}
// function editInfo(id, field, value) {
//     var data = {};
//     data["id"] = id;
//     data[field] = value;
//     data["username"] = username;
//     $.ajax({
//         type: "POST",
//         url: httpurl + "/updateManyFindInfoById",
//         cache: false,
//         headers: {
//             "Access-Control-Allow-Origin": "*",
//             "Access-Control-Allow-Methods": "*",
//             "Access-Control-Allow-Headers": "Authorization",
//             "Authorization": GetTokenInfo()
//         },
//         contentType : 'application/json',
//         data: data,
//         success: function (data) {
//             top.layer.closeAll();
//             top.layer.alert(data.msg);
//
//         }
//     });
// }

function addInfo(id) {
    $.ajax({
        url: httpurl + "/selectPevaluateSettingById",
        cache: false,
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "Authorization",
            "Authorization": GetTokenInfo()
        },
        contentType: 'application/json',
        data: { "id": id },
        success: function(data) {
            if (data.rtnCode == 200) {
                addInfos(data.data);
            } else {
                top.layer.alert("未找到任何数据,请刷新后再试!!");
            }
        }
    });
}

function search() {
    console.log('点击搜索');
    var openid = $("#openid").val() || '';
    var name = $("#username").val();
    var phone = $("#phoneNum").val();
    console.log(openid, name, phone);
    getlistdata(openid, name, phone)
}